package com.sctvcloud.dongqu.ui.adapter.holder;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Pair;
import android.view.View;

import com.gridsum.tracker.GridsumWebDissector;
import com.ruihang.generalibrary.ui.widget.CustomEXImageView;
import com.ruihang.generalibrary.ui.widget.CustomFontTextView;
import com.ruihang.generalibrary.utils.DateUtils;
import com.sctvcloud.dongqu.R;
import com.sctvcloud.dongqu.base.BaseActivity;
import com.sctvcloud.dongqu.beans.LiveItem;
import com.sctvcloud.dongqu.ui.activities.LivingActivity;
import com.sctvcloud.dongqu.ui.utils.GlideUtil;

import java.util.ArrayList;
import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;

public class LiveHomeCountDownHolder extends BaseAbsHolder<LiveItem> implements View.OnClickListener {

    @BindView(R.id.item_live_item_img)
    protected CustomEXImageView img;
    @BindView(R.id.item_live_time)
    protected CustomFontTextView countDownTime;
    @BindView(R.id.item_live_item_title)
    protected CustomFontTextView title;
    @BindView(R.id.item_live_label_date)
    protected CustomFontTextView date;
    @BindView(R.id.item_live_label_channel)
    protected CustomFontTextView channel;

    private LiveItem liveItem;

    public LiveHomeCountDownHolder(Context context, View itemView) {
        super(context, itemView);
        itemView.setOnClickListener(this);
    }

    @Override
    protected void initInject() {
        ButterKnife.bind(this, itemView);
    }

    @Override
    public void setData(LiveItem liveItem) {
        this.liveItem = liveItem;
        if (null != DateUtils.getDataDate(liveItem)) {
            countDownTime.setText(" 预报 " + DateUtils.formatDate(DateUtils.getDataDate(liveItem), "") + " 开始 ");
        } else {
            countDownTime.setText(R.string.live_advance);
        }
        title.setText(liveItem.getTitle());
        date.setText(DateUtils.formatDate(DateUtils.getDataDate(liveItem), ""));
        channel.setText(liveItem.getLiveSource());
        if (!TextUtils.isEmpty(liveItem.getLiveImg())) {
            GlideUtil.loadImgLarge169Def(context, liveItem.getLiveImg(), img);
        }
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent(context, LivingActivity.class);
//		if (liveItem != null && liveItem.isLiving()) {
        intent.putExtra(BaseActivity.EX_URL, liveItem.getLiveUrl());
        intent.putExtra(BaseActivity.EX_DATA, liveItem.getLiveImg());

        intent.putExtra(LivingActivity.ADVANCE_TYPE, true);
        intent.putExtra(LivingActivity.ADVANCE_TITLE, liveItem.getTitle());
        intent.putExtra(LivingActivity.ADVANCE_IMAGE_URL, liveItem.getLiveImg());
        intent.putExtra(LivingActivity.ADVANCE_START_TIME, liveItem.getLiveTime());

        pairs.add(new Pair<String, String>("位置", "直播预约"));

        ArrayList<Pair<String, String>> pairArrayList = new ArrayList<>();
        pairArrayList.addAll(pairs);
        pairArrayList.add(new Pair<String, String>("行为类型", "点击"));
        if (fragment != null) {
            GridsumWebDissector.getInstance().trackEvent(fragment, "", liveItem.getTitle(), "", 200, pairArrayList);
        } else if (activity != null) {
            GridsumWebDissector.getInstance().trackEvent(activity, "", liveItem.getTitle(), "", 200, pairArrayList);
        }
        HashMap<String, String> map = new HashMap<>();
        for (Pair<String, String> pair : pairs) {
            map.put(pair.first, pair.second);
        }
        intent.putExtra("TAG", map);
        context.startActivity(intent);

    }
}
